//
//  EmailLoginViewController.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/23.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "EmailLoginViewController.h"
#import "EmailRegisteredViewController.h"

#import "LoginViewModel.h"

@interface EmailLoginViewController()<UITextFieldDelegate>

@property (nonatomic, strong) LoginViewModel *loginVM;

@property (weak, nonatomic) IBOutlet UIButton *emailBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *forgetPwdBtn;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@property (weak, nonatomic) IBOutlet UILabel *noAccountLabel;

@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;

@end

@implementation EmailLoginViewController

#pragma mark - VC Life

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self showInfoMsg];
    [self inputAction];
    [self outPutAction];
}

- (void)showInfoMsg {
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
   
    _phoneNumberTextField.placeholder = kLocalizedString(@"请输入您的邮箱号码", nil);
    _pwdTextField.placeholder = kLocalizedString(@"请输入您的密码", nil);
    [_emailBtn setTitle:kLocalizedString(@"手机号登录", nil) forState:(UIControlStateNormal)];
    [_forgetPwdBtn setTitle:kLocalizedString(@"忘记密码", nil) forState:(UIControlStateNormal)];
    [_loginButton setTitle:kLocalizedString(@"登录", nil) forState:(UIControlStateNormal)];
    _loginButton.backgroundColor = UIColorFromRGB(0xff5101);
    _noAccountLabel.text =  kLocalizedString(@"还没有账号？", nil);
    [_registerBtn setTitle:kLocalizedString(@"立即注册", nil) forState:(UIControlStateNormal)];
}

#pragma mark - Input
- (void)inputAction {
    @weak(self)
    //点击登录
    [[_loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self_weak_ startClearBgColorLoading];
        [self_weak_.loginVM.loginCommand execute:@{@"email":self_weak_.phoneNumberTextField.text,@"password":self_weak_.pwdTextField.text}];
    }];
    
    //邮箱登录
    [[self.emailBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self_weak_.navigationController popViewControllerAnimated:YES];
    }];
    
    //立即注册
    [[self.registerBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        RegisteredViewController *registerVC = [story instantiateViewControllerWithIdentifier:@"RegisteredVC"];
        registerVC.showType = 0;
        [self_weak_.navigationController pushViewController:registerVC animated:YES];
    }];
    
    //忘记密码
    [[self.forgetPwdBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        EmailRegisteredViewController *emailVC = [story instantiateViewControllerWithIdentifier:@"emailRegisteredVC"];
        emailVC.showType = 1;
        [self_weak_.navigationController pushViewController:emailVC animated:YES];
    }];
}

#pragma mark - OutPut
- (void)outPutAction {
    @weak(self)
    //登录成功
    [self.loginVM.loginCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        [self_weak_ stopLoading];
        [((AppDelegate*)[UIApplication sharedApplication].delegate) changeRootVC];
        
        [[NSUserDefaults standardUserDefaults] setValue:self_weak_.phoneNumberTextField.text forKey:@"emailAddress"];
        [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"area"];
        [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"phoneNumber"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }];
    
    [self.loginVM.loginCommand.errors subscribeNext:^(NSError * _Nullable x) {
        [self_weak_ stopLoading];
    }];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSMutableString *s = [NSMutableString stringWithFormat:@"%@",textField.text];
    [s insertString:string atIndex:range.location];
    if ([NSString isBlankString:string]) {
        return YES;
    }
    if (([NSString isContainsEmoji:string] && ![NSString isNineKeyBoard:string]) || (string.isEmoji && ![NSString isNineKeyBoard:string]) || (([NSString hasEmoji:string] && ![NSString isNineKeyBoard:string]))) {
        return NO;
    }
    return YES;
}

#pragma mark - Lazy
- (LoginViewModel *)loginVM {
    if (!_loginVM) {
        _loginVM = [[LoginViewModel alloc]init];
    }
    return _loginVM;
}

@end
